package com.morder.util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.util.Xml;

import com.morder.Info;

public class MOrderHelper {
  
  public static ArrayList<Info> getInfoList(int pageIndex) {
    ArrayList<Info> list = new ArrayList<Info>();
    String url = "http://open.client.lashou.com/api/detail/city/%E6%B7%B1%E5%9C%B3/p/" + pageIndex + "/r/10";
    String dataString = getContentFormUrl(url);
    // System.out.println("dataString: " + dataString);
    try {
      list = parseInfoString(dataString);
    } catch (XmlPullParserException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return list;
  }
  
  private static ArrayList<Info> parseInfoString(String input) throws XmlPullParserException, IOException {
    ArrayList<Info> list = new ArrayList<Info>();
    Info info = null;
    InputStream ins = new ByteArrayInputStream(input.getBytes());
    XmlPullParser parser = Xml.newPullParser();
    parser.setInput(new InputStreamReader(ins));

    int event = parser.getEventType();
    boolean stopParse = false;
    while ((event != XmlPullParser.END_DOCUMENT) && !stopParse) {
      switch (event) {
      case XmlPullParser.START_DOCUMENT:
        break;
      case XmlPullParser.START_TAG:
        String parserName = parser.getName();
        if ("url".equals(parserName)) {
          info = new Info();
          String id = parser.getAttributeValue(0);
          info.setId(id);
        } else if ("small_image".equals(parserName)) {
          String image = parser.nextText();
          info.setImage(image);
        } else if ("value".equals(parserName)) {
          String value = parser.nextText();
          info.setValue(value);
        }
        break;
      case XmlPullParser.END_TAG:
        if ("url".equals(parser.getName())) {
          list.add(info);
        }
        break;
      default:
        break;
      }
      event = parser.next();
    }
    System.out.println("list: " + list);
    return list;
  }
  
  private static String getContentFormUrl(String url) {
    String result = "";
    try {
      DefaultHttpClient client = new DefaultHttpClient();
      HttpUriRequest request = new HttpGet(url);
      HttpResponse response = client.execute(request);
      HttpEntity entity = response.getEntity();
      int status = response.getStatusLine().getStatusCode();

      if (status == HttpStatus.SC_OK) {
        // result = EntityUtils.toString(entity);
        result = EntityUtils.toString(entity, "utf-8");
      }
      client.getConnectionManager().shutdown();
      return result;
    } catch (Exception e) {
      Log.e("NetHelper", "读取数据失败" + e.toString());
      return "";
    }
  }

  public static boolean networkIsAvailable(Context context) {
    ConnectivityManager cManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cManager.getActiveNetworkInfo();
    if (info == null) {
      return false;
    }
    if (info.isConnected()) {
      return true;
    }
    return false;
  }
  
}
